// pages/bidding-detail/bidding-detail.js
import { Bidding } from '../../models/bidding'
import { Publish } from '../../models/other'
import { Collect } from '../../models/collect'
import Dialog from '../../miniprogram_npm/@vant/weapp/dialog/dialog'
import collectFun from '../../util/collect'
import queryParams from '../../util/query'

const collectModel = new Collect()
const biddingModel = new Bidding()
const publishModel = new Publish()

const app = getApp()
Page({
  data: {
    data: {},
    id: '',
    isIphoneX: app.globalData.isIPhoneX,
    supplyList: [],
    supplyStatus: 0, // 0: 什么都没发布过 1: 有通过 2: 全部拒绝 部分通过的就不考虑了
    showCheckSupply: false,
    selectedSupply: '',
    showBackHome: false,
    showDialog: false,
  },

  onLoad (options) {
    wx.showLoading({
      title: '加载中'
    })
    this.setData({
      id: options.id || queryParams(decodeURIComponent(options.q), 'id'),
      showBackHome: !!options.ald_share_src
    })
    this.getDetail()
  },

  getDetail () {
    biddingModel.detail({
      id: this.data.id
    }, res => {
      if (res.auditStatus == 0) {
        // setTimeout(() => {
        //   Notify({
        //     message: '正在审核中，请耐心等待回复！',
        //     duration: 30000,
        //     background: '#46E3BC'
        //   })
        // }, 1000)
        Dialog.alert({
          message: '正在审核中，请耐心等待回复！',
        }).then(() => {
          // on close
        })
      }
      wx.hideLoading()
      this.setData({
        data: res
      })
    })
  },

  toNewPage (event) {
    this.setData({
      showDialog: false
    })
    let path = event.currentTarget.dataset.path
    wx.navigateTo({
      url: `/pages/${path}/${path}`
    })
  },

  onCollect() {
    collectFun('招标', this.data.data.id, this.data.data.collectState, () => {
      this.setData({
        'data.collectState': !this.data.data.collectState
      })
    })
  },


  getRecruit () {
    return new Promise((resolve, reject) => {
      collectModel.myPublish({
        type: '供应商'
      },res => {
        if (res.rows.length) {
          let statusArray = []
          res.rows.forEach(item => {
            if (item.auditStatus == 0 || item.auditStatus == 2 || item.auditStatus == 3) {
              statusArray.push(2)
            }
            if (item.auditStatus == 1) {
              statusArray.push(1)
            }
          })
          if (statusArray.includes(2)) {
            this.setData({
              supplyStatus: 2
            })
          }
          if (statusArray.includes(1)) {
            this.setData({
              supplyStatus: 1
            })
          }
          this.setData({
            supplyList: res.rows.filter(item => item.auditStatus == 1)
          })
        }
        resolve(true)
      })
    })
  },

  async delivery () {
    // 投递信息
    if(!wx.getStorageSync('userToken')){
      wx.showToast({
        title: '请先登录',
        icon: 'none'
      })
      wx.navigateTo({
        url: `/pages/login/login`
      })
      return
    }
    await this.getRecruit()
    switch (this.data.supplyStatus) {
      case 0:
        this.setData({
          showDialog: true
        })
        break
      case 1:
        this.setData({
          showCheckSupply: true
        })
        break
      case 2:
        wx.showToast({
          title: '请耐心等待审核通过后投递！',
          icon: 'none'
        })
        break
    }
  },

  cancelDialog() {
    this.setData({
      showDialog: false
    })
  },


  onCheckedSupply (e) {
    const id = e.currentTarget.dataset.item.id
    this.setData({
      selectedSupply: id
    })
  },

  cancelSupply () {
    this.setData({
      showCheckSupply: false
    })
  },

  confirmSupply () {
    this.setData({
      showCheckSupply: false
    })
    biddingModel.delivery({
      biddingId: this.data.data.id,
      supplierId: this.data.selectedSupply,
    }, res => {
      wx.showToast({
        title: res,
        icon: 'none'
      })
      wx.requestSubscribeMessage({
        tmplIds: ['6E4h4hY0mRpdOPOZLZj6EGV26CKx-3BoSJ3wwLcs3t4', 'gqiUsZicpSK12MajZO_xtAITyrFoFgw4A1n6fibfN0w', 'eyzGwf2pUMpnz03Hq27JbBefHa6FLYXIgx8evLxMZlE'],
        complete (res) {
        }
      })
    })
  },

  cancelRecruit () {
    this.setData({
      selectedSupply: '',
      showCheckSupply: false
    })
  },

  previewImg() {
    wx.previewImage({
      current: this.data.data.avatar,
      urls: [this.data.data.avatar]
    })
  },

  onReady () {
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    publishModel.shareStick({
      contentId: this.data.id,
      type: '招标'
    })
    return {
      title: '求购' + this.data.data.title,
      path: `/pages/bidding-detail/bidding-detail?id=${this.data.id}&otherId=${wx.getStorageSync('userInfo').user_id}&time=${+new Date()}`
    }
  }
})
